package TUILayer;
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import java.util.ArrayList;
import ModelLayer.*;


/**
 * Write a description of class KundeUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class KundeUI
{
    private KundeCtr kunCtr;
    private Kunde kunder;

    public KundeUI()
    {
        kunCtr = new KundeCtr();
    }
    
    public void start()
    {
        kundeMenu();
        kunCtr = new KundeCtr();
    }
    
    public void kundeMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeKundeMenu();
            if(choice == 1)
            {
                findKunde();
            }
            else{
                if(choice == 2)
                {
                    createKunde();
                }
                else{
                    if(choice == 3)
                    {
                        deleteKunde();
                    }
                    else{
                        if(choice == 4)
                        {
                            updateKunde();
                        }
                        else{
                            if(choice == 5)
                            {
                                listAll();
                            }
                            else{
                                exit = true;
                            }
                        }
                    }
                }
            }
        }
    }
    
    public int writeKundeMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** Kunde Menu *** ");
        System.out.println(" (1) Find kunde");
        System.out.println(" (2) Opret kunde");
        System.out.println(" (3) Slet kunde");
        System.out.println(" (4) Rediger kunde");
        System.out.println(" (5) List alle kunder");
        System.out.println(" (6) Tilbage");
        System.out.println("\n Vælg: ");
        System.out.println();
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public int inputKundeId()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Indtast Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Der skal indtastes et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }
        return id;
    }
    
    public int inputIndexNo()
    {   
        boolean ok = false;
        int index = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Indtast nr.:  ");
           try{
             index = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Der skal indtastes et tal -  prøv igen");
             String input = keyboard.nextLine();
           }
        }
        return index;
    }
    
    public int inputId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Indtast kundens ID:  ");
        int id = keyboard.nextInt();
        return id;
    }
    
    public String inputNavn()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Indtast navn:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputAdresse()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Indtast adresse:  ");
         String adresse = keyboard.nextLine();
         return adresse;
    }
    
    public int inputPostNr()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Indtast postnummer:  ");
         int postNr = keyboard.nextInt();
         return postNr;
    } 
    
    public String inputBy()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Indtast by:  ");
         String by = keyboard.nextLine();
         return by;
    } 
     
    public int inputTlf()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Indtast telefon nummer: ");
        int tlf = keyboard.nextInt();
        return tlf;
    }
    
    public String inputFirma()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Indtast firma:  ");
         String firma = keyboard.nextLine();
         return firma;
    } 
    
    public void showId(int id)
    {
        System.out.println("kundens ID: " + id);
    }
    public void showNavn(String navn)
    { 
        System.out.println("Kundens navn: " + navn);
    }
    public void showAdresse(String adresse)
    { 
        System.out.println("Kundens adresse: " + adresse);
    }

    private void findKunde()
    {
        String navn = inputNavn();
        ArrayList<Kunde> kunde = kunCtr.listKundeNr();
        Kunde kunder = kunCtr.findKunde(navn);
        System.out.println("\n" + kunder.getId() + " " + kunder.getNavn() + " " + kunder.getAdresse() + " " + kunder.getPostNr() + " " + kunder.getBy() + " " + kunder.getTlf() + " " + kunder.getFirma() + "\n");
        
        if(kunder == null)
        {
            System.out.println("Kan ikke finde kunden");
        }
       
    }   
    
    private void listAll()
    {
         ArrayList<Kunde> kunde = kunCtr.listKundeNr();
         int index = 0;
         for(Kunde kunder : kunde)
         {
               System.out.println("\n" + index + " " + kunder.getId() + " " + kunder.getNavn() + " " + kunder.getAdresse() + " " + kunder.getPostNr() + " " + kunder.getBy() + " " + kunder.getTlf() + " " + kunder.getFirma());
               index ++;
         }
         
         System.out.println();
    }
    
    private void createKunde()
    {
        int id = inputId();
        String navn = inputNavn();
        String adresse = inputAdresse();
        int postNr = inputPostNr();
        String by = inputBy();
        int tlf = inputTlf();
        String firma = inputFirma();
        
        kunCtr.createKunde(id, navn, adresse, postNr, by, tlf, firma);
    }
    
    private void deleteKunde()
    {
        ArrayList<Kunde> kunde = kunCtr.listKundeNr();
         int index = 0;
         for(Kunde kunder : kunde)
         {
               System.out.println("\n" + "index nummer :" + index + " " + kunder.getId() + " " + kunder.getNavn() + " " + kunder.getAdresse() + " " + kunder.getPostNr() + " " + kunder.getBy() + " " + kunder.getTlf() + " " + kunder.getFirma());
               index ++;
         }
         
         System.out.println("Indtast id nummeret på kunden du vil slette");
         int id = inputKundeId();
         
         kunCtr.deleteKunde(id);
         System.out.println("Kunden er nu slettet");
         
         
    }
    
    private void updateKunde()
    {
        int id = inputId();
        String navn = inputNavn();
        Kunde kunde = kunCtr.findKundeId(id);
        if(navn.length() > 0 && kunde != null)
        {
            kunde.setNavn(navn);
        }
        String adresse = inputAdresse();
        int postNr = inputPostNr();
        String by = inputBy();
        int tlf = inputTlf();
        String firma = inputFirma();
    }
}
